<%@ page import="com.service.CartService" %>
<%@ page import="java.text.DecimalFormat" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.Arrays" %>
<%@ page import="com.entity.*" %>
<%@ page import="com.Imp.*" %>
<%@ page import="com.mapper.*" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- 新 Bootstrap5 核心 CSS 文件 -->
    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/5.1.1/css/bootstrap.min.css">
    <!--  popper.min.js 用于弹窗、提示、下拉菜单 -->
    <script src="https://cdn.staticfile.org/popper.js/2.9.3/umd/popper.min.js"></script>
    <!-- 最新的 Bootstrap5 核心 JavaScript 文件 -->
    <script src="https://cdn.staticfile.org/twitter-bootstrap/5.1.1/js/bootstrap.min.js"></script>
    <link rel="stylesheet" type="text/css" href="css/orderStyle.css">
</head>
<body>

<div class="container-fluid">
    <div class="row">
        <nav class="navbar navbar-expand-lg navbar-dark bg-dark">


            <div class="container-fluid">
                <a class="navbar-brand" href="#"><img src="img/caidan.png" height="35" width="35">&nbsp菜单</a>
                <ul class="navbar-nav ms-auto mb-2 mb-lg-0">
                    <%
                        String customerId = (String) session.getAttribute("username");
                        CustomerMapper customerMapper = new CustomerMapperImpl();
                        Customer customer = customerMapper.getCustomerByUserName(customerId);
                    %>
                    <li class="nav-item">
                        <a class="nav-link" href="information_customer.jsp">尊贵的顾客&nbsp<%=customer.getName() %>，您好！  </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="login.jsp">退出登录</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="#">关于我们</a>
                    </li>
                </ul>
            </div>
        </nav>
    </div>

    <div class="row">
        <div class="col-md-2 sidebar">
            <div class="sidebar-menu">
                <ul class="custom-list ">
                    <li><a href="main.jsp"><img src="img/main.png" height="18" width="18">&nbsp主页</a></li>
                    <li><a href="storeList.jsp"><img src="img/shop.png" height="18" width="18">&nbsp店铺</a></li>
                    <li><a href="cart.jsp"  style="background-color: #55aaff;"><img src="img/shoppingTrolley.png" height="18" width="18">&nbsp购物车</a></li>
                    <div id="order">
                        <div class="orderlist">
                            <a class="btn" data-bs-toggle="collapse" href="#collapseOne"><img src="img/order.png" height="18" width="18">&nbsp我的订单</a>
                        </div>
                        <%

                            OManagementMapper oManagementMapper_0 = new OManagementMapperImpl();
                            List<OManagement> oManagementList_0 = oManagementMapper_0.getOrdersBycustomerId(customerId);
                            int num1 = 0;
                            int num2 = 0;
                            int num3 = 0;
                            for (OManagement oManagement : oManagementList_0) {
                                if (oManagement.getOrderState().equals("待分配")) {
                                    num1 = num1 + 1;
                                } else if (oManagement.getOrderState().equals("配送中")) {
                                    num2 = num2 + 1;
                                } else if (oManagement.getOrderState().equals("已送达")) {
                                    num3 = num3 + 1;
                                }
                            }
                        %>
                        <div id="collapseOne" class="collapseshow" data-bs-parent="#order">
                            <div class="orderlist-body">
                                <a href="waitingOrders.jsp">待配送（<%= num1 %>）</a>
                            </div>
                        </div>
                        <div id="collapseOne" class="collapseshow" data-bs-parent="#order">
                            <div class="orderlist-body">
                                <a href="deliveringOrders.jsp">配送中（<%= num2 %>）</a>
                            </div>
                        </div>
                        <div id="collapseOne" class="collapseshow" data-bs-parent="#order">
                            <div class="orderlist-body">
                                <a href="deliveredOrders.jsp">已完成（<%= num3 %>）</a>
                            </div>
                        </div>
                    </div>

                    <li><a href="information_customer.jsp"><img src="img/personal.png" height="18" width="18">&nbsp个人信息</a></li>
                    <li></li>
                </ul>
            </div>
        </div>


        <div class="col-md-10 main-content">
            <div class="container">
                我的购物车
                <div class="address" >
                    我的地址：<%= customer.getAddress() %>
                </div>

                <div class="row">
                    <div class="col-md-12">
                        <%-- 获取购物车中的所有店铺信息 --%>
                        <%
                            CartMapper cartMapper = new CartMapperImpl();

                            String userName = (String) session.getAttribute("username");
                            CartService cartService = new CartService();

                            List<Cart> cartList = cartMapper.getCartByCustomerId(userName);
                            StoreMapper storeMapper = new StoreMapperImpl();

                            String[] stores = new String[0];

                            int id=0;

                            for (Cart cart : cartList) {
                                String storeId = cart.getStoreId();
                                Store store = storeMapper.getStoreBystoreId(storeId);
                                int f = 0;
                                for (String str : stores) {
                                    if (str.equals(storeId)) {
                                        f = 1;
                                        break;
                                    }
                                }
                                if(f == 1){ continue;}
                                else{
                                    String[] newArray = Arrays.copyOf(stores, stores.length + 1);
                                    newArray[newArray.length - 1] = storeId;
                                    stores = newArray;
                                }
                        %>
                        <div class="cart-container">
                            <form action="OrderController?op=settltAccount" method="get" name="dishForm" id="form<%=id%>">
                                <table class="table table-striped table-bordered">
                                    <h2><div class="storename" ><img src="img/storename.png" height="38" width="40">&nbsp<%=store.getName()%></div></h2>
                                    <thead>
                                    <tr>
                                        <th>#</th>
                                        <th>菜品图片</th>
                                        <th>菜品名称</th>
                                        <th>单价</th>
                                        <th>数量</th>
                                        <th>小计</th>
                                        <th></th>
                                        <th></th>
                                    </tr>
                                    </thead>

                                    <tbody>
                                    <%
                                        List<Cart> cuisineList = cartMapper.getCartByStoreId(cart.getStoreId(), cart.getCustomerId());
                                        for (Cart cart_1 : cuisineList) {
                                            CuisineMapper cuisineMapper = new CuisineMapperImpl();
                                            Cuisine cuisine = cuisineMapper.getCuisineByCuisineId(cart_1.getCuisineId());
                                    %>
                                    <tr>
                                        <td>
                                            <input type="checkbox" style="zoom:200%" class="selectedItems" name="selectedItems"  value="<%= cart_1.getCuisineId() %>">
                                        </td>
                                        <td>
                                            <img src=<%=cuisine.getPhotoURL()%> height="75" width="75">
                                        </td>
                                        <td>
                                            <%= cart_1.getCuisineName() %>
                                        </td>
                                        <td>￥<%= cart_1.getPrice() %></td>
                                        <td>
                                            <span class="mx-2"><%= cart_1.getAmount() %></span>
                                        </td>
                                        <td>￥<%= new DecimalFormat("0.00").format(cart_1.getPrice() * cart_1.getAmount()) %></td>
                                        <td>
                                            <form action="CartController?op=add_1" method="post">
                                                <input type="hidden" name="cuisineId" value="<%= cart_1.getCuisineId() %>">
                                                <button type="submit" class="btn btn-danger btn-sm">增加</button>
                                            </form>
                                        </td>
                                        <td>
                                            <form action="CartController?op=delete" method="post">
                                                <input type="hidden" name="cuisineId" value="<%= cart_1.getCuisineId() %>">
                                                <button type="submit" class="btn btn-sm btn-primary">减少</button>
                                            </form>
                                        </td>
                                    </tr>
                                    <% } %>
                                    </tbody>
                                </table>

                                <div class="container2 mb-2">
                                    <div class="row justify-content-end">
                                        <div class="col-md-4 col-lg-3 col-xl-2">
                                            <div class="card">
                                                <div class="card-body">
                                                    <h5 class="card-title"><img src="img/js.png" height="25" width="25">&nbsp结算结算</h5>
                                                    <!-- 添加 action 属性 -->
                                                    <input type="hidden" name="storeId" value="<%= cart.getStoreId() %>">
                                                    <button type="button" onclick="settle(<%=id%>)" class="btn btn-primary w-100">去结算</button>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </form>
                        </div>

                        <% id++;} %>

                    </div>
                </div>
            </div>
        </div>
        <footer class="bg-dark text-light text-center py-4">
            <div class="container1">
                <p>&copy; FOODEXPRESS</p>
            </div>
        </footer>
    </div>

    <!-- 模态框 -->
    <div class="modal" id="myModal">
        <div class="modal-dialog">
            <div class="modal-content">

                <!-- 模态框头部 -->
                <div class="modal-header">
                    <h4 class="modal-title">FoodExpress</h4>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>

                <!-- 模态框内容 -->
                <div class="modal-body">
                    请勾选要结算的菜品！
                </div>

                <!-- 模态框底部 -->
                <div class="modal-footer">
                    <button type="button" class="btn btn-primary" data-bs-dismiss="modal">确定</button>
                </div>

            </div>
        </div>
    </div>
</div>
</body>
<%--<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>--%>
<script>
    function settle(id){
        let songId= document.getElementsByName("selectedItems");
        let flag=0;
        //检查是否勾选菜品
        for(let i=0;i<songId.length;i++){
            if(songId[i].checked==true){
                flag=1;
                break;
            }
        }
        if(flag==0){
            let modal = new bootstrap.Modal(document.getElementById('myModal'), {
                keyboard: false
            });
            modal.show();
        }else{
            let myForm=document.getElementById("form"+id)
            myForm.submit();
        }
    }

</script>
</html>
